<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <section>
        <img width="50" src="../assets/mylogo.png" alt="" />
        <span>奇思妙想后台管理系统</span>
      </section>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <section class="toggle-button" @click="toggleCollapse">|||</section>
        <!-- 侧边栏菜单 -->
        <el-menu
          active-text-color="#409fff"
          background-color="#333744"
          text-color="#fff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-sub-menu
            :index="item.id + ''"
            v-for="item in menulist"
            :key="item.id"
          >
            <!-- 一级菜单的模板区域 -->
            <template #title>
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState('/' + subItem.path)"
            >
              <!-- 二级菜单的模板区域 -->
              <template #title>
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-sub-menu>
        </el-menu>
      </el-aside>
      <!-- 右边主体区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { getCurrentInstance, reactive, ref, onMounted, inject } from "vue";
import { useRouter, useRoute } from "vue-router";

export default {
  setup() {
    // 获取vue实例
    const { proxy } = getCurrentInstance();
    // 获取路由
    const router = useRouter();
    const route = useRoute();
    // 注入一个axios
    const axios = inject("axios");

    // 左侧菜单数据
    let menulist = reactive([]);

    // 存储左侧一级菜单的小图标
    const iconsObj = {
      125: "iconfont icon-user",
      103: "iconfont icon-tijikongjian",
      101: "iconfont icon-shangpin",
      102: "iconfont icon-danju",
      145: "iconfont icon-baobiao",
    };

    // 是否折叠
    let isCollapse = ref(false);

    // 被激活的链接地址
    let activePath = ref(route.path);

    // 页面初始化请求数据
    const getUserList = async () => {
      menulist.splice(0, menulist.length);
      const { data: res } = await axios.get("menus");
      if (res.meta.status !== 200) return proxy.$message.error(res.meta.msg);
      res.data.forEach((item) => menulist.push(item));
    };
    onMounted(() => {
      getUserList();
    });

    // 退出按钮
    const logout = () => {
      // 退出登录
      window.sessionStorage.removeItem("token");
      // 退出登录后跳转到登录页面
      router.push("/login");
    };

    // 点击按钮折叠菜单
    const toggleCollapse = () => {
      isCollapse.value = !isCollapse.value;
    };

    // 保存连接的激活状态
    const saveNavState = (path) => {
      // window.sessionStorage.setItem("activePath", activePath);
      activePath.value = path;
    };

    return {
      iconsObj,
      menulist,
      isCollapse,
      activePath,
      logout,
      toggleCollapse,
      saveNavState,
    };
  },
};
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;

  > section {
    display: flex;
    align-items: center;

    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #333744;

  .el-menu {
    border-right: none;
  }
}

.el-main {
  background-color: #eaedf1;
}

.iconfont {
  margin-right: 10px;
}

.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>